import { sort, prop, ascend, uniqBy } from 'ramda';
import Layout from '@/layout';
export default class Menu {
  static convertTree(list) {
    const array = uniqBy(x => x.uuid)(list);
    const sortList = sort(ascend(prop('sort')), array);
    sortList.forEach(r => {
      sortList.forEach((m, i) => {
        if (m.parentUuid && m.parentUuid === r.uuid) {
          if (!r.children) r.children = [];
          r.children.push(m);
        }
      });
    });
    const tempList = sortList.reduce((i, m) => {
      if (m.parentUuid === null || m.parentId === null) {
        i.push(m);
      }
      return i;
    }, []);
    return tempList;
  }

  static convertDicTree(list) {
    list.forEach(r => {
      list.forEach((m, i) => {
        if (m.parentId && m.parentId === r.id) {
          if (!r.children) r.children = [];
          r.children.push(m);
        }
      });
    });
    const tempList = list.reduce((i, m) => {
      if (m.parentId === null) {
        i.push(m);
      }
      return i;
    }, []);
    return tempList;
  }

  static dicFilter(list) {
    const obj = {};
    const array = sort(ascend(prop('id')), list);
    for (let i = 0; i < array.length; i++) {
      obj[array[i].name] = [];
      for (let j = 0; j < array.length; j++) {
        if (array[j].parentId && array[j].parentId === array[i].id) {
          obj[array[i].name].push({
            id: array[j].id,
            label: array[j].supplement,
            value: array[j].value
          });
        }
      }
    }
    for (const key of Object.keys(obj)) {
      if (!obj[key].length) {
        delete obj[key];
      }
    }
    return obj;
  }

  static recursion(list) {
    try {
      const newList = [];
      for (let i = 0; i < list.length; i++) {
        if (list[i].children && list[i].children.length > 0) {
          const json = {
            path: list[i].path,
            name: list[i].uuid,
            component: Layout,
            hidden: list[i].hidden,
            meta: {
              title: list[i].name,
              icon: list[i].icon
            },
            children: []
          };
          if (list[i].parentUuid) {
            json.component = this.asyncView(list[i].template);
          }
          newList.push(json);
          const rtlist = this.recursion(list[i].children);
          const length = newList.length - 1;
          newList[length].children.push(...rtlist);
        } else {
          newList.push({
            path: list[i].path,
            component: this.asyncView(list[i].template),
            name: list[i].uuid,
            hidden: list[i].hidden,
            meta: {
              title: list[i].name,
              icon: list[i].icon
            }
          });
        }
      }
      return newList;
    } catch (error) {
      console.error(error);
    }
  }

  static asyncView(template) {
    return () => import(`@/views/${template}`);
  }

  static convertRoutes(list) {
    const routes = this.recursion(this.convertTree(list));
    routes.push({ path: '*', redirect: '/404', hidden: true });
    console.table(routes);
    return routes;
  }
}
